package com.itboye.lottery.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.itboye.lottery.R;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.BetDetailBean;
import com.itboye.lottery.bean.BetZqDetailBean;
import com.itboye.lottery.bean.NumberBetCodeBean;
import com.itboye.lottery.utils.OnItemClickListener;
import com.itboye.lottery.view.FlowLayout;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by  HONGDA on 2018/6/19.
 */
public class NumberBetDetailListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity context;
    public List<NumberBetCodeBean> mData = new ArrayList<>();
    private LayoutInflater inflater;
    private String type;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public NumberBetDetailListAdapter(Activity context, List<NumberBetCodeBean> mData) {
        this.context = context;
        this.mData = mData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_numberbet_code, parent, false);
        AddressListViewHolder holder = new AddressListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AddressListViewHolder viewHolder = (AddressListViewHolder) holder;
        resetAllView(viewHolder);
        NumberBetCodeBean betBean = mData.get(position);
        if (mData != null) {
            switch (betBean.get_type()) {
                case "单式"://只有红胆蓝胆
                case "复式":
                    if (betBean.getRed_ball() != null && !TextUtils.isEmpty(betBean.getRed_ball())) {
                        viewHolder.ll_touzhu_red.setVisibility(View.VISIBLE);

                        buildData();
                        if (betBean.getRed_ball().contains(",")) {
                            addNumberViews(betBean.getRed_ball().split(","), viewHolder.fl_touzhu_red_code, true);
                        } else {
                            addNumberViews(new String[]{betBean.getRed_ball()}, viewHolder.fl_touzhu_red_code, true);
                        }
                    }
                    if (betBean.getBlue_ball() != null && !TextUtils.isEmpty(betBean.getBlue_ball())) {
                        viewHolder.ll_touzhu_blue.setVisibility(View.VISIBLE);
                        if (betBean.getBlue_ball().contains(",")) {
                            addNumberViews(betBean.getBlue_ball().split(","), viewHolder.fl_touzhu_blue_code, false);
                        } else {
                            addNumberViews(new String[]{betBean.getBlue_ball()}, viewHolder.fl_touzhu_blue_code, false);
                        }
                    }
                    break;
                case "胆拖":
                    if (betBean.getRed_ball() != null && !TextUtils.isEmpty(betBean.getRed_ball())) {
                        viewHolder.ll_touzhu_red.setVisibility(View.VISIBLE);
                        if (betBean.getRed_ball().contains(",")) {
                            addNumberViews(betBean.getRed_ball().split(","), viewHolder.fl_touzhu_red_code, true);
                        } else {
                            addNumberViews(new String[]{betBean.getRed_ball()}, viewHolder.fl_touzhu_red_code, true);
                        }
                    }
                    if (betBean.getRed_ball_drag() != null && !TextUtils.isEmpty(betBean.getRed_ball_drag())) {
                        viewHolder.ll_touzhu_red_tuo.setVisibility(View.VISIBLE);
                        if (betBean.getRed_ball_drag().contains(",")) {
                            addNumberViews(betBean.getRed_ball_drag().split(","), viewHolder.fl_touzhu_red_tuo_code, true);
                        } else {
                            addNumberViews(new String[]{betBean.getRed_ball_drag()}, viewHolder.fl_touzhu_red_tuo_code, true);
                        }
                    }
                    if (betBean.getBlue_ball() != null && !TextUtils.isEmpty(betBean.getBlue_ball())) {
                        viewHolder.ll_touzhu_blue.setVisibility(View.VISIBLE);
                        if (betBean.getBlue_ball().contains(",")) {
                            addNumberViews(betBean.getBlue_ball().split(","), viewHolder.fl_touzhu_blue_code, false);
                        } else {
                            addNumberViews(new String[]{betBean.getBlue_ball()}, viewHolder.fl_touzhu_blue_code, false);
                        }
                    }
                    if (betBean.getBlue_ball_drag() != null && !TextUtils.isEmpty(betBean.getBlue_ball_drag())) {//说明是大乐透
                        viewHolder.ll_touzhu_blue_tuo.setVisibility(View.VISIBLE);
                        viewHolder.tv_touzhu_blue_dan.setText("蓝球(胆):");
                        viewHolder.tv_touzhu_red_dan.setText("红球(胆):");
                        if (betBean.getBlue_ball_drag().contains(",")) {
                            addNumberViews(betBean.getBlue_ball_drag().split(","), viewHolder.fl_touzhu_blue_tuo_code, false);
                        } else {
                            addNumberViews(new String[]{betBean.getBlue_ball_drag()}, viewHolder.fl_touzhu_blue_tuo_code, false);
                        }
                    }

                    break;
            }
        }
        viewHolder.item_playtype.setText("投注方式:" + betBean.get_type());
    }

    private void buildData() {

    }

    public void addNumberViews(String[] codeArray, FlowLayout flowLayout, boolean isRed) {
        flowLayout.removeAllViews();
        for (String s : codeArray) {
            flowLayout.addView(createTextViewForNumBet(s, isRed));
        }
    }

    private TextView createTextViewForNumBet(String s, boolean b) {
        TextView textView = new TextView(context);
        textView.setText(s);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(context.getResources().getColor(R.color.white));
        textView.setBackgroundResource(b ? R.drawable.solid_circle_border_red : R.drawable.solid_circle_border_blue);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(2, 2, 2, 2);
        textView.setLayoutParams(layoutParams);
        return textView;
    }

    private void resetAllView(AddressListViewHolder viewHolder) {
        viewHolder.ll_touzhu_red.setVisibility(View.GONE);
        viewHolder.ll_touzhu_red_tuo.setVisibility(View.GONE);
        viewHolder.ll_touzhu_blue.setVisibility(View.GONE);
        viewHolder.ll_touzhu_blue_tuo.setVisibility(View.GONE);
        viewHolder.tv_touzhu_red_dan.setText("红球:");
        viewHolder.tv_touzhu_blue_dan.setText("蓝球:");
    }

    /*篮球投注表格绘制*/

    /*足球投注表格绘制*/

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void setType(String type) {
        this.type = type;
    }

    public class AddressListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        FlowLayout fl_touzhu_red_code, fl_touzhu_red_tuo_code, fl_touzhu_blue_code, fl_touzhu_blue_tuo_code;
        LinearLayout ll_touzhu_red, ll_touzhu_red_tuo, ll_touzhu_blue, ll_touzhu_blue_tuo;

        TextView tv_touzhu_red_dan, tv_touzhu_blue_dan, item_playtype;

        public AddressListViewHolder(View itemView) {
            super(itemView);
            fl_touzhu_red_code = itemView.findViewById(R.id.fl_touzhu_red_code);
            fl_touzhu_red_tuo_code = itemView.findViewById(R.id.fl_touzhu_red_tuo_code);
            fl_touzhu_blue_code = itemView.findViewById(R.id.fl_touzhu_blue_code);
            fl_touzhu_blue_tuo_code = itemView.findViewById(R.id.fl_touzhu_blue_tuo_code);

            ll_touzhu_red = itemView.findViewById(R.id.ll_touzhu_red);
            ll_touzhu_red_tuo = itemView.findViewById(R.id.ll_touzhu_red_tuo);
            ll_touzhu_blue = itemView.findViewById(R.id.ll_touzhu_blue);
            ll_touzhu_blue_tuo = itemView.findViewById(R.id.ll_touzhu_blue_tuo);

            tv_touzhu_red_dan = itemView.findViewById(R.id.tv_touzhu_red_dan);
            tv_touzhu_blue_dan = itemView.findViewById(R.id.tv_touzhu_blue_dan);
            item_playtype = itemView.findViewById(R.id.item_playtype);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, getAdapterPosition());
            }
        }
    }

}
